﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace InventoryManager
{
    public partial class ViewRequisitionForm_2_UserControl : UserControl
    {
        public event FormChangedHandler FormChangedEvent;

        private string _connectionString;

        private int _selectedRowIndex;
        private int _selectedColumnIndex;
        private string _requisitionID;

        private bool _invoiceMode;

        public ViewRequisitionForm_2_UserControl(string connectionString, object[] param, int width, int height, bool invoiceMode)
        {
            InitializeComponent();

            _connectionString = connectionString;
            this.Width = width;
            this.Height = height;

            _selectedRowIndex = -1;
            _selectedColumnIndex = -1;
            _requisitionID = "";

            _invoiceMode = invoiceMode;

            SearchResultDataGridView.DataSource = (DataTable)param[0];

            TitleLabel.Text = "ผลการค้นหา จากวันที่ " + (string)param[1] + " ถึงวันที่ " + (string)param[2];
        }

        private void OpenReport(int reportType, int orderID)
        {
            ShowPriceForm showPriceForm = new ShowPriceForm();
            ShowReportForm showReportForm;

            switch (showPriceForm.ShowDialog())
            {
                case DialogResult.OK:
                    showReportForm = new ShowReportForm(_connectionString);
                    showReportForm.setReportToShow(reportType, orderID, 
                        !showPriceForm.ShowPriceCheckBox.Checked, !showPriceForm.ShowWeightCheckBox.Checked, null);
                    showReportForm.LoadReport();
                    showReportForm.ShowDialog();
                    break;
                default:
                    break;
            }
        }

        private void SearchResultDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                DataGridViewCell clickedCell;
                if(_invoiceMode)
                    clickedCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[0];
                else
                    clickedCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[1];

                ShowReportForm showReportForm = new ShowReportForm(_connectionString);
                string requisitionID = clickedCell.Value.ToString();

                if(_invoiceMode)
                    FormChangedEvent("INVOICE_ADD_3", new object[] { requisitionID });
                else
                    OpenReport(2, Int32.Parse(requisitionID));
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            if(_invoiceMode)
                FormChangedEvent("INVOICE_VIEW_1", null);
            else
                FormChangedEvent("REQUISITION_VIEW_1", null);
        }

        private void SearchResultDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    DataGridViewCell clickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[_selectedColumnIndex];

                    // Here you can do whatever you want with the cell
                    this.SearchResultDataGridView.CurrentCell = clickedCell;  // Select the clicked cell, for instance

                    // Get mouse position relative to the vehicles grid
                    var relativeMousePosition = SearchResultDataGridView.PointToClient(Cursor.Position);

                    // Show the context menu and set ID
                    DataGridViewCell pickedCell;
                    if(_invoiceMode)
                        pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[0];
                    else
                        pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[1];
                    _requisitionID = pickedCell.Value.ToString();

                    if(_invoiceMode)
                        InvoiceRightClickMenu.Show(SearchResultDataGridView, relativeMousePosition);
                    else
                        RequisitionRightClickMenu.Show(SearchResultDataGridView, relativeMousePosition);
                }
            }
        }

        void editRequisitionAndInvoiceForm_EditFormClosedEvent(int recordType)
        {
            if (recordType == 2)
                FormChangedEvent("REQUISITION_SELECTION", null);
        }

        private void ROpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenReport(2, Int32.Parse(_requisitionID));
        }

        private void REditRequisitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRequisitionAndInvoiceForm editRequisitionAndInvoiceForm = new EditRequisitionAndInvoiceForm(_connectionString, 2, Int32.Parse(_requisitionID));
            editRequisitionAndInvoiceForm.EditFormClosedEvent += new EditFormClosedHandler(editRequisitionAndInvoiceForm_EditFormClosedEvent);
            editRequisitionAndInvoiceForm.ShowDialog();
        }

        private void IOpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenReport(2, Int32.Parse(_requisitionID));
        }

        private void IAddInvoiceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormChangedEvent("INVOICE_ADD_3", new object[] { _requisitionID });
        }
    }
}
